/*
 * Copyright (c) 1997-2012 InfoReach, Inc. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * InfoReach ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with InfoReach.
 *
 * CopyrightVersion 2.0
 *
 */

package com.inforeach.util.string;

import java.util.Arrays;
import java.util.List;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

/**
 * @author dmytros
 *
 */
public class SimpleSplitUnite_utest
{
    SplitUniteHelper sut_;

    @Before
    public void setUp()
    {
        sut_ = new SplitUniteHelper(new SimpleSplitUnite(';', '!'));
    }

    void check(String str, String... elems)
    {
        List<String> lst = Arrays.asList(elems);
        Assert.assertEquals("unite", str, sut_.unite(lst));
        Assert.assertEquals("split", lst, sut_.split(str));
    }

    @Test
    public void testSimpleTwo()
    {
        check("abc;def", "abc", "def");
    }

    @Test
    public void testSimpleOne()
    {
        check("abc", "abc");
    }

    @Test
    public void testEscapeSep()
    {
        check("ab!;cd", "ab;cd");
    }

    @Test
    public void testEscapeEsc()
    {
        check("ab!!!;cd", "ab!;cd");
    }

    @Test
    public void testEscapeEscAtHead()
    {
        check("!;abcd", ";abcd");
        check("!!!!!;abcd", "!!;abcd");
    }

    @Test
    public void testEscapeEscAtTail()
    {
        check("abcd!;", "abcd;");
        check("abcd!!!!!;", "abcd!!;");
        check("abcd!!!", "abcd!!!");
    }

    @Test
    public void testNoEscapeEsc()
    {
        check("ab!!!cd", "ab!!!cd");
    }

    @Test
    public void testEmptyList()
    {
        check("");
    }

    @Test
    public void testListWithOneEmpty()
    {
        check(";", "");
    }

    @Test
    public void testListWithAllEmpties()
    {
        check(";;;", "", "", "");
    }

    @Test
    public void testListWithOneEmptyAtHead()
    {
        check(";a", "", "a");
    }

    @Test
    public void testListWithOneEmptyAtTail()
    {
        check("b;", "b", "");
    }

    @Test
    public void testListWithOneEmptyInside()
    {
        check("ab;;cde", "ab", "", "cde");
    }

    @Test
    public void javadoc0()
    {
        check("abcdef", "abcdef");
    }

    @Test
    public void javadoc1()
    {
        check("abc;def", "abc", "def");
    }

    @Test
    public void javadoc2()
    {
        check("abc!;def", "abc;def");
    }

    @Test
    public void javadoc3()
    {
        check("abc!!;def", "abc!", "def");
    }

    @Test
    public void javadoc4()
    {
        check("abc;def!", "abc", "def!");
    }

    @Test
    public void javadoc4a()
    {
        check("abc;!def", "abc", "!def");
    }

    @Test
    public void javadoc5()
    {
        check("abc!!!;def", "abc!;def");
    }

    @Test
    public void javadoc5a()
    {
        check("abc!;!def", "abc;!def");
    }

    @Test
    public void javadoc6()
    {
        check("abc!!!!;def", "abc!!", "def");
    }

    @Test
    public void javadoc7()
    {
        check("abc!!!def", "abc!!!def");
    }

    @Test
    public void javadoc8()
    {
        check("");
    }

    @Test
    public void javadoc9()
    {
        check(";", "");
    }

    @Test
    public void javadocA()
    {
        check(";;", "", "");
    }

    @Test
    public void javadocB()
    {
        check(";;;", "", "", "");
    }

    @Test
    public void javadocC()
    {
        check("abc;", "abc", "");
    }

    @Test
    public void javadocD()
    {
        check(";def", "", "def");
    }

    @Test
    public void javadocE()
    {
        check("abc;;def", "abc", "", "def");
    }

    @Test
    public void javadocF()
    {
        check(";!;!;;!;!", "", ";;", ";!");
    }

    @Test
    public void testException()
    {
        try
        {
            new SimpleSplitUnite('?', '?');
            Assert.fail("IllegalArgumentException is expected");
        }
        catch (IllegalArgumentException exc)
        {
            Assert.assertEquals("sep == esc == \\u003f (?)", exc.getMessage());
        }
    }
}
